﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CSA.Lib;

namespace CSA.Operator
{
    public class CustomOperator : ICSOperator
    {                
        #region Fields

        CSPopulation m_OperatePopulation=null;     
        String m_CurUsedFunctionName="";
        Hashtable m_FunctionNames = new Hashtable();
        CSFuntionInfo m_CustomAntibodyOperator = null;

        #endregion

        #region Properties

        public String Name
        {
            get { return "CustomOperator"; }
        }

        public String Caption
        {
            get { return global::CSA.Operator.TextString.CustomOperatorCaption; }
        }

        public String Description
        {
            get { return global::CSA.Operator.TextString.CustomOperatorDes; }
        }

        public String Library
        {
            get { return "CSA.Operator"; }
        }

        public Hashtable Get_Parameters(String nFunctionName)
        {
            String nValidateName = "";
            if (nFunctionName.Trim() == "" || !m_FunctionNames.Contains(nFunctionName))
            {
                nValidateName = "DefaultCustomMethod";

            }
            else
            {
                nValidateName = nFunctionName;
            }
            CSFuntionInfo nDefulatParas = (CSFuntionInfo)m_FunctionNames[nValidateName];
            return nDefulatParas.Parameters;
        }

        public OperatorType OperatorType
        {
            get { return OperatorType.enumCustomOperator; }
        }
        public Hashtable FunctionNames
        {
            get { return m_FunctionNames; }
        }

        public bool UseCustomAntibodyOperator
        {
            get { return true; }
        }

        public CSFuntionInfo CustomAntibodyOperator
        {
            get { return m_CustomAntibodyOperator; }
            set { m_CustomAntibodyOperator = value; }
        }

        #endregion

        #region Methods

        public CustomOperator()
        {
            InitialParameters();
        }

        public bool Execute(CSPopulation nOperatePopulation, ref String nErrorStr, String nMethodName = "")
        {          
            m_OperatePopulation = nOperatePopulation;
               if (nMethodName.Trim() == "" || !m_FunctionNames.Contains(nMethodName))
            {
                m_CurUsedFunctionName = "DefaultCustomMethod";
            }
            else
            {
                m_CurUsedFunctionName = nMethodName;
            }
            CSFuntionInfo nDefulatParas = (CSFuntionInfo)m_FunctionNames[m_CurUsedFunctionName];
            if (m_CurUsedFunctionName == "DefaultCustomMethod")
            {
                CSParameter CrossRate = (CSParameter)nDefulatParas.Parameters["OperatorName"];
                String nFunctionName = (String)CrossRate.Value;
                ICSOptimizationProblem nProblem = m_OperatePopulation.ProblemInfo;
                List<CSAntibody> nNewPop = m_OperatePopulation.NewAntibodies;
                Int32 nIt;
                for (nIt = 0; nIt < nNewPop.Count; ++nIt)
                {
                    CSAntibody nAb = nNewPop[nIt];
                    bool nSuccess = nProblem.CustomOperator(ref nAb, nFunctionName);
                    if (nSuccess == false)
                    {
                        return false;
                    }
                }
                nNewPop.Sort(new AntibodyCompare());
            }
            return true;
        }

        public void InitialParameters()
        {
            m_FunctionNames.Clear();
            CSFuntionInfo m_DefaultFunctionInfo = new CSFuntionInfo();
            m_DefaultFunctionInfo.Caption = "CustomOptFunction";
            m_DefaultFunctionInfo.Description = "CustomOptFunction";
            m_DefaultFunctionInfo.Name = "DefaultCustomMethod";
            Hashtable nStandardFunctionPara = new Hashtable();
            m_DefaultFunctionInfo.Parameters = nStandardFunctionPara;
            m_FunctionNames.Add(m_DefaultFunctionInfo.Name, m_DefaultFunctionInfo);
        }

        #endregion
    }
}
